package com.lanou3g.gifttalk.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lanou3g.gifttalk.R;
import com.lanou3g.gifttalk.entity.BaseEntity;
import com.lanou3g.gifttalk.entity.ChannelData;
import com.lanou3g.gifttalk.http.HttpManager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Risky57 on 2017/6/27.
 */

public class HomeFragment extends Fragment {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;

    private HomePagerAdapter mPagerAdapter;

    @Nullable
    @Override
    public View onCreateView (LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        mViewPager = (ViewPager) view.findViewById(R.id.view_pager);
    }

    @Override
    public void onActivityCreated (@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPagerAdapter = new HomePagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(mPagerAdapter);
        Call<BaseEntity<ChannelData>> channels = HttpManager.getApi().getChannels();
        channels.enqueue(new Callback<BaseEntity<ChannelData>>() {
            @Override
            public void onResponse (Call<BaseEntity<ChannelData>> call, Response<BaseEntity<ChannelData>> response) {
                mPagerAdapter.addAll(response.body().getData().getChannels());
        mTabLayout.setupWithViewPager(mViewPager);
            }

            @Override
            public void onFailure (Call<BaseEntity<ChannelData>> call, Throwable t) {

            }
        });
    }
}
